import time
from selenium.webdriver.common.by import By
from base import BasePage, BaseHandle
# from utils import utils_driver, choice_channel, is_exist
from hmAutoTest.utils import UtilsDriver


class AuditPage(BasePage):
    def __init__(self):
        super().__init__()
        self.title = (By.CSS_SELECTOR, "[placeholder='请输入:文章名称']")
        self.channel = (By.CSS_SELECTOR, "[placeholder='请选择状态']")
        self.query_btn = (By.CSS_SELECTOR, ".el-button--primary")
        self.pass_btn = (By.XPATH, "//tbody/tr/td[8]/div/button[2]")
        self.confirm_btn = (By.CSS_SELECTOR, ".e-button--primary")
        self.end_time = (By.XPATH, "//*[@placeholder='选择结束时间']")

    def find_title(self):
        return self.get_element(self.title)

    def find_channel(self):
        return self.get_element(self.channel)

    def find_query_btn(self):
        return self.get_element(self.query_btn)

    def find_pass_btn(self):
        return self.get_element(self.pass_btn)

    def find_confirm_btn(self):
        return self.get_element(self.confirm_btn)

    def find_end_time(self):
        return self.get_element(self.end_time)


class AuditHandle(BaseHandle):
    def __init__(self):
        self.audit_page = AuditPage()

    def input_title(self, title):
        self.input_text(self.audit_page.find_title(), title)

    def choice_status(self, status):
        UtilsDriver.choice_channel(self.audit_page.driver, self.audit_page.find_channel(), status)

    def input_end_time(self, end_time):
        self.input_text(self.audit_page.find_end_time(), end_time)

    def click_query_btn(self):
        self.audit_page.find_query_btn().click()

    def click_pass_btn(self):
        self.audit_page.find_pass_btn().click()

    def click_confirm_btn(self):
        self.audit_page.find_confirm_btn().click()


class AuditProxy:
    def __init__(self):
        self.audit_handle = AuditHandle()
        self.driver = UtilsDriver.get_mis_driver()

    def audit_article(self, title, status, end_time):
        self.audit_handle.input_title(title)
        self.audit_handle.choice_status(status)
        self.audit_handle.input_end_time(end_time)
        time.sleep(1)
        self.audit_handle.click_query_btn()
        time.sleep(1)
        self.audit_handle.click_pass_btn()
        time.sleep(1)
        self.audit_handle.click_confirm_btn()

    def audit_article_pass(self, title):
        self.audit_handle.input_title(title)
        self.audit_handle.choice_status("审核通过")
        time.sleep(1)
        self.audit_handle.click_query_btn()
        time.sleep(1)
        # return is_exist(self.driver, title)
        return UtilsDriver.is_exist(self.driver, title)